/*
 * Copyright 2025 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

import ".."
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtQuick.Window 2.15
import org.ukui.quick.items 1.0 as UkuiItems
import org.ukui.quick.platform 1.0

Window {
    id: window

    property int dragX: 0
    property int dragY: 0
    property bool dragging: false
    property bool isCentered: true
    property string selectedName: ""
    property int currentIndex: -1
    property int layoutMargins: 24

    function handleDragStarted(posX, posY) {
        dragX = posX;
        dragY = posY;
        dragging = true;
    }

    function handleDragging(posX, posY) {
        if (dragging) {
            window.x += posX - dragX;
            window.y += posY - dragY;
        }
    }

    function handleCloseClicked() {
        window.close();
    }

    function handleDragEnd() {
        dragging = false;
    }

    function cancelAction() {
        listView.model = null;
        searchImModel.filterText = "";
        window.destroy();
    }

    function saveAction() {
        if (selectedName !== "") {
            imModel.addInputMethod(selectedName);
            imModel.updateCurrentGroupInputMethodList();
        }
        listView.model = null;
        searchImModel.filterText = "";
        window.destroy();
    }

    width: 480
    height: 560
    modality: Qt.ApplicationModal
    flags: Qt.FramelessWindowHint
    color: "transparent"

    UkuiItems.Background {
        id: addInputMethodBackground

        anchors.fill: parent
        color: Theme.base()
        radius: 6
        clip: true
        Theme.onPaletteChanged: {
            color = Theme.base();
        }
        border.width: 1
        border.color: Qt.rgba(0, 0, 0, 0.1)

        MouseArea {
            anchors.fill: parent
            enabled: searchTextInput.searchInputFocusActive
            onClicked: searchTextInput.searchInputFocus = false
        }

        CustomHeader {
            id: customHeader

            width: parent.width
            height: dragAreaHeight
            anchors.top: parent.top
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.leftMargin: 8
            anchors.rightMargin: 4
            headerTitle: qsTr("Add")
            z: 1
            onDragStarted: {
                handleDragStarted(posX, posY);
            }
            onDragging: {
                handleDragging(posX, posY);
            }
            onDragEnded: {
                handleDragEnd();
            }
            onCloseClicked: {
                handleCloseClicked();
            }
        }

        ColumnLayout {
            id: imsColumn

            anchors.top: customHeader.bottom
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.topMargin: 8
            Layout.fillHeight: true
            spacing: 16

            SearchTextInput {
                id: searchTextInput

                Layout.leftMargin: layoutMargins
                Layout.rightMargin: layoutMargins
                Layout.fillWidth: true
                Layout.preferredHeight: 32
                onSearchTextChanged: {
                    searchImModel.filterText = text;
                }
            }

            Rectangle {
                Layout.leftMargin: layoutMargins
                Layout.rightMargin: layoutMargins
                Layout.fillWidth: true
                Layout.preferredHeight: 334
                border.width: 1
                border.color: Qt.rgba(0, 0, 0, 0.1)
                clip: true

                color:"transparent"
                ListView {
                    id: listView

                    anchors.fill: parent
                    model: searchImModel

                    delegate: ItemDelegate {
                        width: listView.width - scrollBar.width
                        height: 36
                        hoverEnabled: true

                        Text {
                            height: parent.height
                            width: parent.width
                            text: model.name
                            elide: Qt.ElideRight
                            verticalAlignment: Text.AlignVCenter
                            color: index === currentIndex ? Theme.highlightedText() : Theme.buttonText()
                            leftPadding: 8
                        }

                        background: Rectangle {
                            id: background

                            radius: 6
                            color: {
                                if (index === currentIndex)
                                    return Theme.highlight();
                                else if (hovered)
                                    return Theme.midLight();
                                else
                                    index % 2 ? Theme.button() : "transparent";
                            }

                            MouseArea {
                                id: mouseArea

                                anchors.fill: parent
                                onClicked: {
                                    currentIndex = index;
                                    selectedName = model.uniqueName;
                                }
                            }

                        }

                    }

                    ScrollBar.vertical: ScrollBar {
                        id: scrollBar

                        policy: ScrollBar.AlwaysOn
                        width: 8
                        height: 100
                        anchors.right: parent.right
                        anchors.top: parent.top

                        contentItem: Rectangle {
                            color: Qt.rgba(0, 0, 0, 0.1)
                            radius: 2
                            opacity: 1
                        }

                    }

                }

            }

            RowLayout {
                spacing: 0
                Layout.fillWidth: true
                Layout.leftMargin: layoutMargins
                Layout.rightMargin: layoutMargins

                CheckBox {
                    id: checkBox

                    opacity: searchTextInput.text === ""? 1:0
                    Layout.topMargin: 2.5
                    implicitWidth: 16
                    implicitHeight: 16
                    checked: true
                    onCheckedChanged: {
                        searchImModel.onlyShowCurrentLanguage = checked;
                    }

                    indicator: Rectangle {
                        implicitWidth: 16
                        implicitHeight: 16
                        radius: 4
                        color: checkBox.checked ? Theme.highlight() : Theme.button()
                        border.width: 1
                        border.color: Qt.rgba(0, 0, 0, 0.1)

                        UkuiItems.Icon {
                            id: checkImage

                            anchors.fill: parent
                            visible: checkBox.checked
                            source: checkBox.checked ? "object-select-symbolic" : ""
                        }

                        MouseArea {
                            anchors.fill: parent
                            onClicked: {
                                checkBox.checked = !checkBox.checked;
                            }
                        }

                    }

                }

                UkuiItems.StyleText {
                    id: label

                    opacity: searchTextInput.text === ""? 1:0
                    text: qsTr("Only Show Current Language")
                    Layout.leftMargin: 16
                }

                Item {
                    Layout.fillWidth: true
                }

            }

            UkuiItems.Background {
                Layout.fillWidth: true
                height: 1
                Layout.leftMargin: addInputMethodBackground.border.width
                Layout.rightMargin: addInputMethodBackground.border.width
                Layout.preferredHeight: 1
                color: Theme.button()
                Theme.onPaletteChanged: {
                    color = Theme.button();
                }
            }

            RowLayout {
                Layout.fillWidth: true
                Layout.leftMargin: layoutMargins
                Layout.rightMargin: layoutMargins
                spacing: 8

                Item {
                    Layout.fillWidth: true
                }

                CustomButton {
                    width: 88
                    height: 36
                    labelText: qsTr("Cancel")
                    onCustomButtonClicked: cancelAction()
                }

                CustomButton {
                    width: 88
                    height: 36
                    enableHighlight: true
                    labelText: qsTr("OK")
                    Layout.alignment: Qt.AlignRight
                    onCustomButtonClicked: saveAction()
                }

            }

        }

    }

}
